package models

import "gorm.io/gorm"

// 商家表
type Shop struct {
	gorm.Model
	Title      string  `gorm:"type:varchar(20);comment:'商铺名称'"`
	Score      int64   `gorm:"type:int(3);comment:'评分'"`
	Sales      int64   `gorm:"type:int(5);comment:'销量'"`
	Distance   string  `gorm:"type:varchar(20);comment:'距离'"`
	Types      string  `gorm:"type:varchar(10);comment:'品类'"`
	Price      float32 `gorm:"type:decimal(10,2);comment:'配送费'"`
	BePrice    float32 `gorm:"type:decimal(10,2);comment:'起送价'"`
	Promotions string  `gorm:"type:varchar(20);comment:'优惠活动'"`
	Status     int64   `gorm:"type:int;default:1;comment:默认1 为在线 2为已歇业"`
}

func (s *Shop) ShopList(sortWord, keyWord string, priceWord float32) (listShop []*Shop, err error) {
	query := DB.Model(&Shop{})

	if keyWord != "" {
		query = query.Where("types like ? OR promotions like ?", "%"+keyWord+"%", "%"+keyWord+"%")
	}

	if priceWord != 0 {
		query = query.Where("price =? OR db_price =?", priceWord, priceWord)
	}

	if sortWord == "score" {
		query = query.Order("score desc")
	} else if sortWord == "sales" {
		query = query.Order("sales desc")
	} else if sortWord == "distance" {
		query = query.Order("distance desc")
	} else {
		query = query.Order("created_at desc")
	}

	err = query.Where("status = 1").Find(&listShop).Error
	return
}

func (s *Shop) GetShopByID(shopId int64) error {
	return DB.Where("id =?").Limit(1).Find(&s).Error
}
